2023年 7月 9日
内容纲要
1.1 setup 函数1.2 组合式 API1. reactive 函数2. 修改 reactive 数据3. ref 函数4. 修改 ref 数据5. 将对象赋值给 ref2.1 基于模板中的表达式统计商品信息2.2 计算属性的基本语法2.3 基于计算属性实现加法运算2.4 基于计算属性改造购物车案例2.5 计算属性的缓存 vs 方法3.1 侦听器的基本语法3.2 停止侦听器3.3 侦听的数据源类型1. 侦听 ref 数据源2. 侦听 reactive 数据源3. 侦听 reactive 中某个属性的变化(属性值为对象)4. 基于 getter 侦听 reactive 中某个属性的替换操作(属性值为对象)5. 基于 getter 侦听 reactive 中某个值类型数据的变化6. 侦听多个数据源组成的数组3.4 深层侦听器3.5 即时回调的侦听器3.6 回调的触发时机3.7 watchEffect()1. watch 侦听器的缺点2. watchEffect()3. watch() vs watchEffect()4.1 绑定 HTML 的 class1. 绑定单个 class2. 动态切换单个 class3. 基于计算属性动态切换单个 class4. 绑定 class 对象5. 把 class 对象封装为 reactive 响应式数据6. 把 class 对象封装为 computed 计算属性7. 绑定 class 数组4.2 绑定 style 内联样式1. 绑定对象2. 绑定计算属性3. 绑定数组5.1 访问模板引用5.2 v-for 中的模板引用5.3 函数模板引用
版权归作者 ©刘龙宾 所有,本文章未经作者允许,禁止私自转载!
在前面的课程中,我们曾强调过:使用 data() 函数声明响应式的数据、在 methods 节点下声明事件处理器,都是 Vue2 中的旧语法(选项式API)。这种旧语法的存在,主要是为了防止断崖式升级造成的老用户流失。
在 Vue3 中,推出了组合式 API 的新概念,它可以极大提高功能模块的封装性和复用性。因此,官方推荐所有用户优先使用组合式 API。
在今后的开发中,为了维护老项目,我们可能会不得不用到选项式 API。除此之外,在开发新项目时,推荐大家优先使用组合式 API。
setup() 函数是 Vue3 中组合式 API 的入口,它的语法格式如下:
xxxxxxxxxx101const { createApp } = Vue23const app = createApp({4 setup() {5 // 给模板使用的数据、事件处理器等,都要通过这个 return 的对象暴露给模板6 return {}7 }8})910app.mount('#app')例如,在 setup return 的对象中,声明一个数据 name:
xxxxxxxxxx121const { createApp } = Vue23const app = createApp({4 setup() {5 // 给模板使用的数据、事件处理器等,都要通过这个 return 的对象暴露给模板6 return {7 name: 'liulongbin'8 }9 }10})1112app.mount('#app')紧接着,在模板中即可使用名为 name 的数据了:
xxxxxxxxxx41<!-- 模板 -->2<div id="app">3 <h1>{{ name }}</h1>4</div>注意:刚才定义的 name,并非是响应式的数据,所以基于 app._instance.proxy.name 修改它的值后,不会触发视图的重新渲染。
组合式 API 是 Vue3 提供的一系列函数。例如,可以使用 reactive() 和 ref() 这两个函数,来声明响应式的数据。
reactive 函数用来定义响应式的数据对象,它的使用步骤如下:
xxxxxxxxxx161// 1. 从 Vue 上解构出 reactive 函数2const { createApp, reactive } = Vue34const app = createApp({5 setup() {6 // 2. 调用 reactive 函数,定义响应式的数据7 const user = reactive({ name: 'zs' })89 // 3. 把响应式的数据,挂载到 return 的对象上,供模板使用10 return {11 user12 }13 }14})1516app.mount('#app')接下来,就可以在模板中使用响应式的数据 user 啦:
xxxxxxxxxx41<!-- 模板 -->2<div id="app">3 <h1>{{ user.name }}</h1>4</div>如果想要点击按钮,修改 user.name 的值,我们可以在 setup 中定义事件的处理函数,并挂载到 return 的对象上,供模板使用:
xxxxxxxxxx141setup() {2 const user = reactive({ name: 'liulongbin' })34 // 1. 定义处理函数5 const changeUserName = () => {6 user.name = 'escook'7 }89 return {10 user,11 // 2. 把处理函数,暴露给模板使用12 changeUserName13 }14}最后,在模板中,为 <button> 按钮绑定点击事件:
xxxxxxxxxx61<!-- 模板 -->2<div id="app">3 <h1>{{ user.name }}</h1>4 <!-- 3. 绑定事件处理器 -->5 <button @click="changeUserName">按钮</button>6</div>reactive 函数的缺点:只能为引用类型的数据创建响应式数据对象,无法将值类型的数据转化为响应式数据。
ref 函数用来将值类型的数据转化为响应式数据,它的基本用法如下:
xxxxxxxxxx161// 1. 从 Vue 中结构出 ref 函数2const { createApp, ref } = Vue34const app = createApp({5 setup() {6 // 2. 调用 ref 函数,创建响应式数据 count,其初始值为 07 const count = ref(0)89 return {10 // 3. 把响应式数据 count,暴露给模板使用11 count12 }13 }14})1516app.mount('#app')紧接着,就可以在模板中使用 count 了:
xxxxxxxxxx41<!-- 模板 -->2<div id="app">3 <h1>{{ count }}</h1>4</div>如果想要点击按钮,修改 count 的值,则需要在 setup 函数中定义事件的处理函数,并挂载到 return 的对象上,供模板使用:
xxxxxxxxxx141setup() {2 const count = ref(0)34 // 1. 定义处理函数(注意:在 js 中必须使用 .value 来访问 ref 声明的响应式数据)5 const increase = () => {6 count.value++7 }89 return {10 count,11 // 2. 把处理函数,暴露给模板使用12 increase13 }14}最后,在模板中,为 <button> 按钮绑定点击事件:
xxxxxxxxxx61<!-- 模板 -->2<div id="app">3 <h1>{{ count }}</h1>4 <!-- 3. 绑定事件处理器 -->5 <button @click="increase">+1</button>6</div>注意:使用 ref 声明的响应式数据,在 js 中必须使用 .value 进行访问。在模板中不必使用 .value 进行访问,因为在模板中使用时会自动对 ref 数据进行解包。
ref 函数除了接收值类型的数据之外,还可以将对象类型的数据转化为响应式数据。例如:
xxxxxxxxxx201setup() {2 // 1. 把对象赋值给 ref 作为初始值3 const info = ref({ name: 'liulongbin', age: 18 })45 // 2. 定义处理函数6 const changeInfo = () => {7 // 通过 info.value 分别为每个属性赋值8 info.value.name = 'escook'9 info.value.age = 221011 // 直接给 info.value 赋新值12 // info.value = { name: 'escook', age: 22 }13 }1415 // 3. 向模板暴露数据16 return {17 info,18 changeInfo19 }20}在模板中,直接使用 info 即可,因为模板中会对 ref 数据自动解包:
xxxxxxxxxx51<!-- 模板 -->2<div id="app">3 <h1>{{ info.name }}今年{{info.age}}岁了。</h1>4 <button @click="changeInfo">修改</button>5</div>注意:如果 ref() 处理的是对象类型的数据,则内部会调用 reactive() 函数将对象数据转为响应式数据,然后挂载为 .value 属性。
最佳实践:尽量使用 ref() 来定义响应式数据,因为它既可以处理值类型数据,又可以处理对象类型的数据。
计算属性可以避免程序员在模板中编写大段的 JavaScript 表达式,从而提高代码的整洁度和可维护性。
定义购物车中的商品数据如下:
xxxxxxxxxx181const { createApp, ref } = Vue23const app = createApp({4 setup() {5 // 购物车中的商品数据6 const goods = ref([7 { id: 1, name: '红烧肉盖饭', price: 26, count: 1 },8 { id: 2, name: '小酥肉', price: 12, count: 1 },9 { id: 3, name: '冰镇可乐', price: 6, count: 1 }10 ])1112 return {13 goods14 }15 }16})1718app.mount('#app')在模板中渲染购物车中的商品数据,并基于模板中的表达式计算商品的数量、总价、运费:
xxxxxxxxxx171<div id="app">2 <ul>3 <li v-for="item in goods" :key="item.id">4 <p>商品名称:{{item.name}}</p>5 <p>单价:{{ item.price }}</p>6 <p>购买数量:{{ item.count }}</p>7 </li>8 </ul>910 <hr>11 <div>12 <p>总计:</p>13 <p>商品数量:{{ goods.reduce((total, item) => total += item.count, 0) }}</p>14 <p>商品总价:{{ goods.reduce((total, item) => total += item.price * item.count, 0) }}元</p>15 <p>配送费:{{ goods.reduce((total, item) => total += item.price * item.count, 0) >= 50 ? '免配送费' : '5元' }}</p>16 </div>17</div>编写样式,为商品添加分割线:
xxxxxxxxxx31li+li {2 border-top: 1px solid #efefef;3}在步骤2中,我们发现模板中掺杂了较为复杂的表达式;而且在商品总价和配送费中,两次用到了商品的总价,但每次都需要重新计算总价,无法实现数据的复用。
基于这样的原因,我们可以利用 computed 计算属性,把模板中的表达式运算,封装到 JS 中,从而达到简化模板和实现数据复用的目的。
从 Vue 对象上解构出 computed 函数,它用来定义计算属性:
xxxxxxxxxx11const { computed } = Vue调用 computed 函数,同时向 computed 中传递一个箭头函数作为参数:
xxxxxxxxxx31const res = computed(() => {2 return 计算的结果3})而且,在箭头函数中,必须 return 一个计算的结果。如果忘记了 return,则是一个无效的计算属性。
computed 函数的返回值,是一个 ref 的响应式对象,可通过 .value 访问其值。如果在模板中访问,会自动进行解包:
xxxxxxxxxx11<div>{{ res }}</div>通过 ref 定义响应式数据 n1 和 n2,通过 computed 定义计算属性 result:
xxxxxxxxxx191const { createApp, ref, computed } = Vue2const app = createApp({3 setup() {4 // 响应式 ref 数据5 const n1 = ref(0)6 const n2 = ref(0)78 // 计算属性,依赖于 n1 和 n2 的变化,会自动运算并返回最新的结果9 const result = computed(() => n1.value + n2.value)1011 return {12 n1,13 n2,14 result15 }16 }17})1819app.mount('#app')在模板中使用计算属性:
xxxxxxxxxx71<div id="app">2 <input type="number" v-model="n1">3 <span>+</span>4 <input type="number" v-model="n2">5 <span>=</span>6 <span>{{ result }}</span>7</div>注意:当计算属性依赖的响应式数据发生变化时,会自动对计算属性重新求值。
定义购物车中的商品数据如下:
xxxxxxxxxx181const { createApp, ref } = Vue23const app = createApp({4 setup() {5 // 购物车中的商品数据6 const goods = ref([7 { id: 1, name: '红烧肉盖饭', price: 26, count: 1 },8 { id: 2, name: '小酥肉', price: 12, count: 1 },9 { id: 3, name: '冰镇可乐', price: 6, count: 1 }10 ])1112 return {13 goods14 }15 }16})1718app.mount('#app')分析模板的结构,我们可以得到如下的结论:
所以,我们可以基于计算属性,分别计算出以上3个数据的值,代码如下:
xxxxxxxxxx301// 1. 导入 computed 函数2const { createApp, ref, computed } = Vue34const app = createApp({5 setup() {6 // 购物车中的商品数据7 const goods = ref([8 { id: 1, name: '红烧肉盖饭', price: 26, count: 1 },9 { id: 2, name: '小酥肉', price: 12, count: 1 },10 { id: 3, name: '冰镇可乐', price: 6, count: 1 }11 ])1213 // 2.1 计算属性:商品总数量14 const total = computed(() => goods.value.reduce((t, item) => t += item.count, 0))15 // 2.2 计算属性:商品的总价格16 const amount = computed(() => goods.value.reduce((t, item) => t += item.price * item.count, 0))17 // 2.3 计算属性:运费18 const freight = computed(() => amount.value >= 50 ? '免配送费' : '5元')1920 return {21 goods,22 // 3. 把计算属性暴露给模板使用23 total,24 amount,25 freight26 }27 }28})2930app.mount('#app')在模板中,使用使用这三个计算属性的值即可:
xxxxxxxxxx61<div>2 <p>总计:</p>3 <p>商品数量:{{ total }}</p>4 <p>商品总价:{{ amount }}元</p>5 <p>配送费:{{ freight }}</p>6</div>思考:如果在模板中,想要把实际的支付金额(商品总价 + 运费)显示出来,用计算属性该如何实现?
我们发现,如果把计算属性的业务逻辑封装到 function 中,也能实现相同的结果,例如计算两个数之和的案例:
xxxxxxxxxx221const { createApp, ref } = Vue2const app = createApp({3 setup() {4 const n1 = ref(0)5 const n2 = ref(0)67 // 1. 定义一个用于计算的方法8 const getResult = () => {9 console.log('触发了 function 的执行')10 return n1.value + n2.value11 }1213 return {14 n1,15 n2,16 // 2. 向模板暴露这个方法17 getResult18 }19 }20})2122app.mount('#app')接下来,在模板中调用这个方法,显示计算的结果:
xxxxxxxxxx71<div id="app">2 <input type="number" v-model="n1">3 <span>+</span>4 <input type="number" v-model="n2">5 <span>=</span>6 <span>{{ getResult() }}</span>7</div>注意:使用 function 并不会缓存计算的结果。当我们在模板中多次调用这个 function 时,每次使用都会触发的的执行,哪怕每次计算的结果都是一模一样的:
xxxxxxxxxx141<div id="app">2 <input type="number" v-model="n1">3 <span>+</span>4 <input type="number" v-model="n2">5 <span>=</span>6 <!-- 第1次调用方法 -->7 <span>{{ getResult() }}</span>89 <hr>10 <!-- 第2次调用方法 -->11 <div>{{ getResult() }}</div>12 <!-- 第3次调用方法 -->13 <div>{{ getResult() }}</div>14</div>所以 function 总是会在每次调用时重新执行计算的过程,不会对计算的结果进行缓存。
而 computed 计算属性就很好的实现了缓存的功能:当计算属性中所依赖的响应式数据没有发生变化时,多次使用计算属性,会立即返回之前计算的结果,这极大的提高了计算的性能。
计算属性的优点:1. 能缓存计算的结果 2. 能简化模板的代码
侦听器允许我们监视响应式数据的变化,并触发回调函数的执行,在回调函数的形参中,可以接收 newValue 和 oldValue 两个形参,分别代表变化后的新值和变化前的旧值。
从 Vue 对象上解构出 watch 函数:
xxxxxxxxxx11const { watch } = Vue在 setup 函数中,调用 watch 函数,声明侦听器:
xxxxxxxxxx11watch(要监视的响应式数据, (新值, 旧值) => { /* 回调函数的操作 */ })例如:
xxxxxxxxxx271const { createApp, ref, watch } = Vue23const app = createApp({4 setup() {5 // 1. 从本地存储中读取初始值6 const initValue = localStorage.getItem('count') || 07 // 2. 将初始值转为整数后,赋值给 count8 const count = ref(parseInt(initValue))910 const add = (step) => {11 count.value += step12 }1314 // 3. 监听 count 的变化,并把新值存储到本地15 watch(count, (newValue) => {16 // BOM 的 API17 localStorage.setItem('count', newValue)18 })1920 return {21 count,22 add23 }24 }25})2627app.mount('#app')对应的模板为:
xxxxxxxxxx61<div id="app">2 <h1>{{ count }}</h1>3 <button @click="add(1)">+1</button>4 <button @click="add(2)">+2</button>5 <button @click="add(3)">+3</button>6</div>调用 watch 函数会创建一个侦听器,并返回一个终止侦听器的函数,语法格式如下:
xxxxxxxxxx41const unwatch = watch()23// 当侦听器不再需要时,可调用 unwatch 停止它4unwatch()我们可以改造一下前面的例子,当 count >= 20 时,我们希望停止侦听器的执行,并且移除本地存储,代码如下:
xxxxxxxxxx101// 监听 count 的变化,并把新值存储到本地2const unwatch = watch(count, (newValue) => {3 if (newValue >= 20) {4 console.log('数值>=20了')5 unwatch() // 停止侦听器6 localStorage.removeItem('count') // 移除本地存储的数据7 return8 }9 localStorage.setItem('count', newValue)10})watch 的第一个参数可以是不同形式的“数据源”:它可以是一个 ref (包括计算属性)、一个响应式对象、一个 getter 函数、或多个数据源组成的数组。
xxxxxxxxxx131setup() {2 // 1. 定义 ref 类型的响应式数据3 const x = ref(0)45 // 监听 ref 数据的变化6 watch(x, (newValue, oldValue) => {7 console.log(newValue, oldValue)8 })910 return {11 x12 }13}xxxxxxxxxx161setup() {2 // 1. 定义 reactive 类型的响应式数据3 const obj = reactive({ count: 0 })45 // 2. 监听 reactive 数据的变化6 watch(obj, (newValue, oldValue) => {7 // 注意:这里的 newValue 和 oldValue 是变化前后的同一个对象8 // 所以,它们里面的数据完全一样9 console.log(newValue.count, oldValue.count)10 console.log(newValue, oldValue)11 })1213 return {14 obj15 }16}定义 reactive 类型的响应式数据 obj,其中 obj.info 是一个对象:
xxxxxxxxxx11const obj = reactive({ count: 0, info: { name: 'zs', age: 18 } })如果想侦听 obj.info 下每个属性的变化,并得到变化后的新 obj.info 对象,则可以使用 watch 进行如下的侦听:
xxxxxxxxxx51watch(obj.info, (newValue, oldValue) => {2 // 注意:这里的 newValue 和 oldValue 是变化前后的同一个对象3 // 所以,它们里面的数据完全一样4 console.log(newValue, oldValue)5})当我们修改 obj.info.name 的值,或修改 obj.info.age 的值,都会触发此侦听器。得到的 newValue 是变化后新的 obj.info 对象。
定义 reactive 类型的响应式数据 obj,其中 obj.info 是一个对象:
xxxxxxxxxx11const obj = reactive({ count: 0, info: { name: 'zs', age: 18 } })如果想侦听 obj.info 对象的替换操作,例如,在定时器中把 obj.info 替换为一个新对象:
xxxxxxxxxx31setTimeout(() => {2 obj.info = { username: 'ls', gender: '男' }3}, 1000)此时,使用 watch(obj.info, 回调函数) 无法侦听到对象的替换操作。必须使用 getter 才能侦听到对象的替换操作:
xxxxxxxxxx51watch(() => obj.info, (newValue, oldValue) => {2 // 注意:这里的 newValue 和 oldValue 是变化前后的不同的对象3 // 所以,它们里面的数据是不同的4 console.log(newValue, oldValue)5})注意:所谓的 getter,指的就是通过箭头函数来访问响应式对象中的某个具体属性的取值操作。
定义 reactive 类型的响应式数据 obj,其中 obj.info 是一个对象:
xxxxxxxxxx11const obj = reactive({ count: 0, info: { name: 'zs', age: 18 } })如果想侦听 obj.count 的变化,则不能使用 watch(obj.count, 回调函数) 的方式进行侦听。因为 obj.count 返回的是具体的值,不是响应式的数据。因此,必须使用 getter 才能侦听到对象中值类型数据的变化:
xxxxxxxxxx51// 注意:reactive 中值类型的数据,只能通过 getter 进行侦听2watch(() => obj.count, (newValue, oldValue) => {3 // 其中,newValue 值变化后的新值;oldValue 是变化前的旧值4 console.log(newValue, oldValue)5})定义 ref 和 reactive 类型的响应式数据如下:
xxxxxxxxxx41// ref 类型的响应式数据2const x = ref(0)3// reactive 类型的响应式数据4const obj = reactive({ count: 0, info: { name: 'zs', age: 18 } })调用 watch 声明侦听器时,提供一个要侦听的数据源的数组,当其中任何一个数据发生变化时,都会触发回调函数的执行:
xxxxxxxxxx41watch([x, () => obj.count], ([newX, newCount], [oldX, oldCount]) => {2 console.log(newX, newCount)3 console.log(oldX, oldCount)4})假设有如下的响应式数据:
xxxxxxxxxx11const obj = reactive({ count: 0, info: { name: 'zs', age: 18 } })如果定义了 watch(obj.info, 回调函数) 侦听器,则只能侦听到 obj.info.name 或 obj.info.age 的修改操作,无法侦听到 obj.info 对象的替换操作。
如果定义了 watch(() => obj.info, 回调函数) 侦听器,则只能侦听到 obj.info 对象的替换操作,无法侦听到某个具体属性的修改操作。也就是说,目前的情况下鱼和熊掌不可兼得。
如果既想侦听到对象的替换操作,又想侦听到对象下某个具体属性的修改操作。此时可以给 watch 侦听器提供第3个参数,它是一个配置对象,通过 deep: true 选项即可实现我们的目的,语法格式如下:
xxxxxxxxxx41watch(() => obj.info, (newValue, oldValue) => {2 // 1. 当触发 obj.info 对象的替换操作时,会触发此回调函数的执行3 // 2. 当修改 obj.info.name 或 obj.info.age 时,也会触发此回调函数的执行4}, { deep: true })watch 默认是懒执行的:仅当数据源变化时,才会执行回调。但在某些场景中,我们希望在创建侦听器时,立即执行一遍回调。
此时,我们可以通过传入 immediate: true 选项来强制侦听器的回调立即执行:
xxxxxxxxxx31watch(source, (newValue, oldValue) => {2 // 立即执行,且当 `source` 改变时再次执行3}, { immediate: true })当你更改了响应式状态,它可能会同时触发 Vue 组件更新和侦听器回调。
默认情况下,用户创建的侦听器回调,都会在 Vue 组件更新之前被调用。这意味着你在侦听器回调中访问的 DOM 将是被 Vue 更新之前的状态。
如果想在侦听器回调中能访问被 Vue 更新之后的 DOM,你需要指明 flush: 'post' 选项:
xxxxxxxxxx31watch(source, callback, {2 flush: 'post'3})例如,在 watch 侦听器中,如果想得到最新的 DOM 内容,则需要添加 flush: post 选项,否则得到的是 DOM 更新前的旧内容:
xxxxxxxxxx161setup() {2 // 1. 定义 ref 类型的响应式数据3 const count = ref(0)45 // 2. 侦听 count 的变化6 watch(count, () => {7 // 获取 DOM 元素8 const dom = document.querySelector('#title')9 // 打印内部的文本10 console.log(dom.innerText)11 }, { flush: 'post' })1213 return {14 count15 }16}对应的模板结构为:
xxxxxxxxxx41<div id="app">2 <h1 id="title">count的值是:{{ count }}</h1>3 <button @click="count++">+1</button>4</div>watch 侦听器需要手动维护依赖的列表,如果侦听器所依赖的数据很多,则维护依赖列表的成本会很高,例如:
xxxxxxxxxx351<body>2 <div id="app">3 <input type="text" v-model="p1">4 <input type="text" v-model="p2">5 <input type="text" v-model="p3">6 </div>78 <script src="./vue3.global.js"></script>9 <script>10 const { createApp, ref, watch } = Vue1112 const app = createApp({13 setup() {14 const p1 = ref('')15 const p2 = ref('')16 const p3 = ref('')17 const baseUrl = 'https://api.liulongbin.top/v1/common/get'1819 watch([p1, p2, p3], () => {20 fetch(`${baseUrl}?params1=${p1.value}¶ms2=${p2.value}¶ms3=${p3.value}`)21 .then(res => res.json())22 .then(result => console.log(result.data))23 })2425 return {26 p1,27 p2,28 p329 }30 }31 })3233 app.mount('#app')34 </script>35</body>我们发现,只要 p1, p2, p3 任何一个发生变化,都会触发 watch 的回调函数,而在回调函数中,我们又把这3个数据当做调用接口的参数。
在使用 watch 的时候,我们不得不维护一个 [p1, p2, p3] 的数组,从而明确告诉 watch 要侦听哪些数据的变化。如果要侦听的数据项很多,则每次添加或删除了调用接口的参数时,都需要手动维护这个数组中的元素,非常的麻烦。
例如:下面的例子中,忘记了把 p4 添加到侦听器的依赖数组中,最终导致 p4 变化时,无法触发侦听器的执行:
xxxxxxxxxx81// 2. 但是,却忘记了把 p4 添加到侦听的列表中2// 导致的问题是:哪怕修改了 p4 的值,也不会触发侦听器的执行3watch([p1, p2, p3], () => {4 // 1. 在回调函数中,我们把 p1, p2, p3, p4 当做请求参数5 fetch(`${baseUrl}?params1=${p1.value}¶ms2=${p2.value}¶ms3=${p3.value}¶ms4=${p4.value}`)6 .then(res => res.json())7 .then(result => console.log(result.data))8})为了让用户不必维护侦听器的依赖数组,Vue3 提供了 watchEffect() 函数,它会自动收集响应式数据的依赖关系,例如:
xxxxxxxxxx61watchEffect(() => {2 // 1. 在回调函数中,我们把 p1, p2, p3, p4 当做请求参数3 fetch(`${baseUrl}?params1=${p1.value}¶ms2=${p2.value}¶ms3=${p3.value}¶ms4=${p4.value}`)4 .then(res => res.json())5 .then(result => console.log(result.data))6})另外,watchEffect() 中的回调函数会立即执行,因此不需要指定 immediate: true。
在第一次执行期间,他会自动收集 p1.value, p2.value, p3.value, p4.value 作为依赖。
每当任何一个响应式数据发生变化时,回调函数会再次执行。
因此 watchEffect 让我们不用再明确传递触发回调执行的依赖项。
相同点:
watch 和 watchEffect 都能侦听响应式数据的变化,并重新执行给定的回调函数。
不同点:
watch 只侦听
明确给定的依赖项
,从而触发回调函数的重新执行
watchEffect 会在
回调函数第一次执行
的时候
自动收集响应式数据的依赖关系
每个 HTML 元素都支持使用 class 属性来美化元素的样式,因此 v-bind 指令同样适用于元素的 class 属性,我们可以为 class 动态绑定类名,从而达到动态修改元素样式的目的。
为了演示如何绑定 HTML 的 class,我们先定义如下的一组 class 样式:
xxxxxxxxxx231.red {2 color: red; /* 红色文字 */3}45.bgyellow {6 background-color: yellow; /* 黄色背景 */7}89.thin {10 font-weight: 200; /* 文字粗细 200 */11}1213.fat {14 font-weight: 900; /* 文字粗细 900 */15}1617.big {18 font-size: 100px; /* 文字大小 100px */19}2021.small {22 font-size: 12px; /* 文字大小 12px */23}定义 ref 响应式数据 cname 并把它暴露给模板:
xxxxxxxxxx91setup() {2 // 要绑定给元素的 class 类名3 const cname = ref('')45 // 暴露给模板使用6 return {7 cname8 }9}在模板中,通过 :class="cname" 为元素动态绑定 class 类名:
xxxxxxxxxx11<h1 class="big" :class="cname">这是一个大标题</h1>点击 button 按钮时,给 cname 赋值:
xxxxxxxxxx11<button @click="cname='red'">红色</button>注意:在给元素提供 class 的时候,可以把静态 class 和动态绑定的 class 分别应用于同一个元素。
需求:点击按钮,动态为元素切换 thin 和 fat 的 class 类名。
首先,定义布尔值 flag 并暴露给模板使用:
xxxxxxxxxx91setup() {2 // 定义 ref 类型的布尔值3 const flag = ref(false)45 // 暴露给模板使用6 return {7 flag8 }9}其次,实现点击按钮控制布尔值的切换:
xxxxxxxxxx11<button @click="flag = !flag">Toggle</button>最后,根据布尔值的状态,为元素动态绑定 class 类名:
xxxxxxxxxx11<h1 class="big" :class="flag ? 'thin' : 'fat'">这是一个大标题</h1>注意:这里用到了三元表达式,为了模板内容的简洁,我们也可以把三元表达式封装为 computed 计算属性。
定义 cname 的计算属性:
xxxxxxxxxx121setup() {2 // 定义 ref 类型的布尔值3 const flag = ref(false)4 // 基于 flag.value 的值动态计算要应用给元素的 class 名5 const cname = computed(() => flag.value ? 'thin' : 'fat')67 // 暴露给模板使用8 return {9 flag,10 cname11 }12}在模板中把 cname 动态绑定为元素的 class:
xxxxxxxxxx41<div id="app">2 <h1 class="big" :class="cname">这是一个大标题</h1>3 <button @click="flag = !flag">Toggle</button>4</div>为元素动态绑定 class 对象,可以控制切换元素的多个 class,它的语法格式如下:
xxxxxxxxxx31<!-- 当“布尔值1”为 true 时,会给元素应用“类名A”,否则会移除元素上的“类名A” -->2<!-- 当“布尔值2”为 true 时,会给元素应用“类名B”,否则会移除元素上的“类名B” -->3<h1 :class="{类名A: 布尔值1, 类名B: 布尔值2}">这是一个大标题</h1>例如下面的例子:
定义 ref 类型的响应式数据 isRed 和 isBgYellow,它们的默认值为为布尔值 false:
xxxxxxxxxx111setup() {2 // 1. 定义 ref 类型的响应式数据3 const isRed = ref(false)4 const isBgYellow = ref(false)56 // 2. 暴露给模板使用7 return {8 isRed,9 isBgYellow10 }11}在模板中,点击按钮切换这两个布尔值的状态:
xxxxxxxxxx21<button @click="isRed = !isRed">Toggle Color</button>2<button @click="isBgYellow = !isBgYellow">Toggle Background Color</button>在模板中,为 h1 元素绑定 class 对象:
xxxxxxxxxx11<h1 class="big" :class="{red: isRed, bgyellow: isBgYellow}">这是一个大标题</h1>为了简化模板中的代码,我们还可以把动态绑定的 class 对象,封装为 reactive 类型的响应式数据对象:
xxxxxxxxxx121setup() {2 // 1. 定义 reactive 类型的响应式数据3 const classObj = reactive({4 red: false,5 bgyellow: false6 })78 // 2. 暴露给模板使用9 return {10 classObj11 }12}在模板中,为 h1 元素绑定 class 对象:
xxxxxxxxxx11<h1 class="big" :class="classObj">这是一个大标题</h1>并且在点击按钮的时候,动态切换 classObj.red 和 classObj.bgyellow 的布尔值,从而决定是否把 class 类名应用给元素:
xxxxxxxxxx21<button @click="classObj.red = !classObj.red">Toggle Color</button>2<button @click="classObj.bgyellow = !classObj.bgyellow">Toggle Background Color</button>除了可以把动态绑定的 class 对象封装为 reactive 类型的响应式数据之外,我们还可以把它封装为 computed 计算属性,这也能简化模板中绑定 class 的代码:
xxxxxxxxxx181setup() {2 // 1. 定义 ref 类型的响应式数据3 const isRed = ref(false)4 const isBgYellow = ref(false)56 // 2. 定义计算属性7 const classObj = computed(() => ({8 red: isRed.value,9 bgyellow: isBgYellow.value10 }))1112 // 3. 暴露给模板使用13 return {14 classObj,15 isRed,16 isBgYellow17 }18}在模板中,为 h1 元素动态绑定 class 对象:
xxxxxxxxxx11<h1 class="big" :class="classObj">这是一个大标题</h1>点击按钮时,动态切换布尔值的状态:
xxxxxxxxxx21<button @click="isRed = !isRed">Toggle Color</button>2<button @click="isBgYellow = !isBgYellow">Toggle Background Color</button>我们可以为 class 绑定数组,从而为元素应用多个 class:
xxxxxxxxxx11<h1 class="big" :class="['thin', 'red', 'bgyellow']">这是一个大标题</h1>也可以通过三元表达式,动态控制某个 class:
xxxxxxxxxx11<h1 class="big" :class="['thin', 'red', flag ? 'bgyellow' : '']">这是一个大标题</h1>还可以把三元表达式改造成对象的形式,按需为元素应用 class:
xxxxxxxxxx11<h1 class="big" :class="['thin', 'red', {bgyellow: flag}]">这是一个大标题</h1>为了进一步简化模板中的代码,我们还可以把数组封装为 computed 计算属性:
xxxxxxxxxx121setup() {2 // 1. ref 类型的布尔值3 const flag = ref(false)4 // 2. 计算属性:要应用给元素的样式数组5 const classArr = computed(() => ['thin', 'red', { bgyellow: flag.value }])67 // 3. 暴露给模板使用8 return {9 flag,10 classArr11 }12}然后,只需要为元素的 class 绑定这个计算属性即可:
xxxxxxxxxx11<h1 class="big" :class="classArr">这是一个大标题</h1>:style 支持绑定 JavaScript 的对象值。例如,定义一个 ref 类型的响应式数据:
xxxxxxxxxx91setup() {2 // 1. 定义 ref 类型的响应式数据3 const fs = ref(12)45 // 2. 把数据暴露给模板使用6 return {7 fs8 }9}在模板中为 h1 绑定 style 样式对象:
xxxxxxxxxx51<div id="app">2 <input type="number" v-model="fs">3 <!-- 动态绑定 style 样式对象 -->4 <h1 :style="{color: 'red', fontSize: fs + 'px'}">这是一个大标题</h1>5</div>注意:如果是连字符格式的样式属性,推荐写成小驼峰的形式。
为了保证模板的简洁,我们可以把绑定给 style 的样式对象,封装为 computed 计算属性:
xxxxxxxxxx111setup() {2 const fs = ref(12)3 // 1. 定义计算属性4 const styleObj = computed(() => ({ color: 'red', fontSize: fs.value + 'px' }))56 // 2. 把数据暴露给模板使用7 return {8 fs,9 styleObj10 }11}在模板中,直接使用计算属性:
xxxxxxxxxx51<div id="app">2 <input type="number" v-model="fs">3 <!-- 动态绑定 style 样式对象 -->4 <h1 :style="styleObj">这是一个大标题</h1>5</div>我们还可以给 :style 绑定一个包含多个样式对象的数组。这些对象会被合并后渲染到同一元素上。例如,声明如下的两个 style 样式对象:
xxxxxxxxxx141setup() {2 const fs = ref(12)3 // 1. 定义计算属性4 const styleObj = computed(() => ({ color: 'red', fontSize: fs.value + 'px' }))5 // 2. 定义 style 的样式对象6 const styleObj2 = computed(() => ({ fontWeight: 200, backgroundColor: 'yellow' }))78 // 3. 把数据暴露给模板使用9 return {10 fs,11 styleObj,12 styleObj213 }14}把 styleObj 和 styleObj2 以数组的形式,绑定为 h1 的 style 样式:
xxxxxxxxxx11<h1 :style="[styleObj, styleObj2]">这是一个大标题</h1>虽然 Vue 的声明性渲染模型为你抽象了大部分对 DOM 的直接操作,但在某些情况下,我们仍然需要直接访问底层 DOM 元素。要实现这一点,我们可以使用特殊的 ref attribute:
xxxxxxxxxx11<input ref="input">那么,在 JS 中如何才能够访问到模板中的元素呢?这就需要我们使用 ref() 函数声明一个同名的响应式数据:
xxxxxxxxxx161setup() {2 // 1. 定义同名的 ref 数据3 const input = ref(null)45 watchEffect(() => {6 if (input.value) {7 // 3. 如果 input.value 有值,则调用 DOM 的 focus 函数让其获取焦点8 input.value.focus()9 }10 })1112 // 2. 并把 ref 数据暴露给模板13 return {14 input15 }16}当在 v-for 中使用模板引用时,对应的 ref 中包含的值是一个数组,它将在元素被挂载后包含对应整个列表的所有元素:
给 v-for 循环生成的 li 添加 ref 属性:
xxxxxxxxxx31<ul>2 <li v-for="item in list" :key="item.id" ref="liRefs">{{ item.name }} --- {{item.age}}</li>3</ul>在 JS 中使用 ref() 函数声明同名的数据:
xxxxxxxxxx221setup() {2 // 要渲染的数据3 const list = ref([4 { id: 1, name: 'zs', age: 18 },5 { id: 2, name: 'liulongbin', age: 19 },6 { id: 3, name: 'escook', age: 22 }7 ])89 // 1. 声明同名的 ref 数据10 const liRefs = ref([])1112 watchEffect(() => {13 // 3. 循环每个 DOM 元素,根据索引的奇偶性,为 DOM 元素设置对应的 color 颜色14 liRefs.value.forEach((li, i) => li.style.color = i % 2 === 0 ? 'red' : 'blue')15 })1617 return {18 list,19 // 2. 并把它暴露给模板20 liRefs21 }22}除了使用字符串值作名字,ref attribute 还可以绑定为一个函数,会在每次组件更新时都被调用。该函数会收到元素引用作为其第一个参数:
xxxxxxxxxx11<input :ref="(el) => { /* 将 el 赋值给一个数据属性或 ref 变量 */ }">注意我们这里需要使用动态的 :ref 绑定才能够传入一个函数。当绑定的元素被卸载时,函数也会被调用一次,此时的 el 参数会是 null。你当然也可以绑定一个组件方法而不是内联函数。
例如,在定义响应式数据 flag,用来控制页面上 input 元素的显示和隐藏:
xxxxxxxxxx81setup() {2 // 布尔值3 const flag = ref(true)4 // 把数据暴露给模板使用5 return {6 flag7 }8}在模板中,通过点击按钮,切换 flag 的值:
xxxxxxxxxx21<!-- 点击按钮,切换 flag 的值 -->2<button @click="flag = !flag">Toggle</button>最后,使用 v-if 指令控制 input 的显示和隐藏,并给 input 添加函数模板引用:
xxxxxxxxxx31<!-- 使用 v-if 指令控制 input 元素的显示和隐藏 -->2<!-- 使用 :ref 绑定一个函数,在函数内可以获取到 DOM 的引用 -->3<input v-if="flag" :ref="(el) => el && el.focus()">